wCDB = {military = false, localTime = true}
local ldb = LibStub("LibDataBroker-1.1")
local wC = "|cffe1a500w|cff69ccf0Clock|cffffffff"
local serverHour;
local localHour = "12";
local localMinutes = "00";
local minutes = "00";
local serverSuffix
local localSuffix

local menuFrame = CreateFrame("Frame", "menuFrame", UIParent, "UIDropDownMenuTemplate", "MENU")
local options = {
	{text = "Military Time", value = "military", isNotRadio = true, keepShownOnClick = true, func = (function() if (wCDB.military==false) then wCDB.military = true else wCDB.military = false end end)},
	{text = "Use local time", value = "localTime", isNotRadio = true, keepShownOnClick = true, func = (function() if (wCDB.localTime==false) then wCDB.localTime = true else wCDB.localTime = false end end)},
	{text = nil, value = nil, disabled = true, notCheck = true},
	{text = "Close", value = nil, isNotCheckable = true, keepShowOnClick = false, notCheck = true}
}
local function initialize(self, level)
	local title = UIDropDownMenu_CreateInfo()
	title = UIDropDownMenu_CreateInfo()
	title.text = wC
	title.isTitle = true
	title.notCheckable = true
	UIDropDownMenu_AddButton(title, level)
	local info = UIDropDownMenu_CreateInfo()
	for k,v in pairs(options) do
		info = UIDropDownMenu_CreateInfo()
		if (v.disabled~=nil) then info.disabled = v.disabled end
		if (v.notCheck~=nil) then info.notCheckable = v.notCheck end
		info.text = v.text
		info.value = v.value
		info.isNotRadio = v.isNotRadio
		info.checked = wCDB[v.value]
		info.keepShownOnClick = v.keepShownOnClick
		if (v.func~=nil) then info.func = v.func end
		UIDropDownMenu_AddButton(info, level)
   end
end
UIDropDownMenu_Initialize(menuFrame, initialize, "MENU")
UIDropDownMenu_SetWidth(menuFrame, 100);
UIDropDownMenu_SetButtonWidth(menuFrame, 124)
UIDropDownMenu_JustifyText(menuFrame, "LEFT")

local Broker_wClock = ldb:NewDataObject("Broker_wClock", {
	type = "data source",
	text = ("00:00"),
	value = (localHour..":"..minutes),
	suffix = "pm",
	icon = "interface\\icons\\inv_misc_pocketwatch_01",
	label = wC,
	OnClick = function(self, button)
		if (button=="LeftButton") then
			if (IsShiftKeyDown()==nil) then
				if not (IsAddOnLoaded("Blizzard_TimeManager")) then
					UIParentLoadAddOn("Blizzard_TimeManager")
					ToggleTimeManager()
				else
					ToggleTimeManager()
				end
			elseif (IsShiftKeyDown()==1) then
				if (not IsAddOnLoaded("Blizzard_Calendar")) then
					UIParentLoadAddOn("Blizzard_Calendar")
					Calendar_Toggle()
				else
					Calendar_Toggle()
				end
			end
		elseif (button=="RightButton") then
			ToggleDropDownMenu(1, nil, menuFrame, self, 0, 15)
		end
	end,
	OnTooltipShow = function(tooltip)
		tooltip:AddLine(wC..":")
		tooltip:AddLine(" ")
		tooltip:AddDoubleLine("Server time:",(serverHour..":"..minutes..serverSuffix),nil,nil,nil,1,1,1)
		tooltip:AddDoubleLine("Local time:",(localHour..":"..localMinutes..localSuffix),nil,nil,nil,1,1,1)
		tooltip:AddLine(" ")
		tooltip:AddLine("|cff69ccf0Click|cffffd200 to toggle time manager")
		tooltip:AddLine("|cff69ccf0Shift-Click|cffffd200 to toggle calendar")
		tooltip:AddLine("|cff69ccf0Right-Click|cffffd200 for options")
	end,
})

local function numFormat(num,hour) 
local suffix
	if (hour==true) then
		if (num<12) then suffix = "am" end
		if (num>12) then
			if (wCDB.military==false) then
				num = num -12
				suffix = "pm"
			end 
		end
		if (num==12) then
			suffix = "pm"
		end
		if (num==0) then
			num = 12
			suffix = "am"
		end
	end
	if (num<10) and (hour==false) then
		num = ("0"..num)
	end 
	if (wCDB.military==true) then
		suffix = ""
	end
	return num,suffix
end

local lastUp = 0
local OnUpdate = CreateFrame("Frame")
OnUpdate:SetScript("OnUpdate", function(self, elap)
	lastUp = lastUp + elap
	if (lastUp>1) then
		serverHour, minutes = GetGameTime()
		serverHour,serverSuffix = numFormat(serverHour,true)
		minutes = numFormat(minutes,false)
		local dateInfo = date("*t")
		localHour = dateInfo.hour
		localMinutes = dateInfo.min;
		localHour,localSuffix = numFormat(localHour,true)
		localMinutes = numFormat(localMinutes,false);
		if (wCDB.localTime==false) then
			Broker_wClock.text = (serverHour..":"..minutes..serverSuffix)
			Broker_wClock.value = (serverHour..":"..minutes)
			Broker_wClock.suffix = serverSuffix
		else
			Broker_wClock.text = (localHour..":"..localMinutes..localSuffix)
			Broker_wClock.value = (localHour..":"..localMinutes)
			Broker_wClock.suffix = localSuffix
		end
		lastUp = lastUp - 1
	end
end)